<?php

namespace app\admin\model\bbs;

use think\Model;

class Contents extends Model
{
    // 表名
    protected $name = 'bbs_contents';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'tag_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getTagList()
    {
        return ['1' => __('Tag 1'),'2' => __('Tag 2'),'3' => __('Tag 3')];
    }     


    public function getTagTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['tag']) ? $data['tag'] : '');
        $list = $this->getTagList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function users()
    {
        return $this->belongsTo('app\admin\model\user\User', 'user_id','id', 'user')->
                    field('id,username');
    }

    public function models()
    {
        return $this->belongsTo('app\admin\model\bbs\Models', 'model_id','id')->
        field('id,title');
    }

    public function reply()
    {
        return $this->hasMany('app\admin\model\bbs\Reply', 'content_id','id');
    }
}
